# _*_ coding: UTF-8 _*_
# @Time     : 2021/11/4 11:30
# @Author   : yangshuaishuai
# @Site     : http://www.hzdledu.cn/
# @File     : login.py
# @Software : PyCharm


from selenium import webdriver
import time
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By


class Login():
    def __init__(self, driver):
        self.user_locator = (By.XPATH, '//*[@id="account"]/input')
        self.pwd_locator = (By.XPATH, '//*[@id="password"]/input')
        self.vcode_locator = (By.XPATH, '//*[@id="authCode"]/div/input')
        self.login_locator = (By.XPATH, '//*[@id="root"]/div/div[1]/form/div[5]/div/div/span/button')

        self.driver = driver

        self.wait = WebDriverWait(driver, 30)

    def input_user(self, user):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.user_locator))
            self.driver.find_element(*self.user_locator).clear()
            self.driver.find_element(*self.user_locator).send_keys(user)
        except Exception as e:
            self.driver.get_screenshot_as_file('../log/输入账号错误。png')
            raise e

    def input_pwd(self, pwd):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.pwd_locator))
            self.driver.find_element(*self.pwd_locator).clear()
            self.driver.find_element(*self.pwd_locator).send_keys(pwd)
        except Exception as e:
            self.driver.get_screenshot_as_file('../log/输入密码错误。png')
            raise e

    def input_vcode(self, vcode):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.vcode_locator))
            self.driver.find_element(*self.vcode_locator).clear()
            self.driver.find_element(*self.vcode_locator).send_keys(vcode)
        except Exception as e:
            self.driver.get_screenshot_as_file('../log/输入验证码错误。png')
            raise e

    def click_login(self):
        try:
            self.wait.until(expected_conditions.presence_of_element_located(self.login_locator))
            self.driver.find_element(*self.login_locator).click()
        except Exception as e:
            self.driver.get_screenshot_as_file('../log/点击登录错误。png')
            raise e


if __name__ == "__main__":
    driver = webdriver.Chrome()
    driver.get('http://192.168.6.189/cloud/#/open/login')
    login_page = Login(driver)

    login_page.input_user('admin')
    time.sleep(1)
    login_page.input_pwd('123456')
    time.sleep(1)
    login_page.input_vcode('8888')
    time.sleep(1)
    login_page.click_login()
    time.sleep(1)

    driver.quit()
